//
//  STDIOSToCPPFuncs.cpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/12.
//

extern "C" {
#include "STDIOSToCPPFuncs.h"
}
#include "STDRenderManager.hpp"

/// -------------------------------------------------------------- 引擎生命周期 --------------------------------------------------------------
// 创建引擎
void * stds_gl_createRenderEngine() {
    STDRenderManager *render = new STDRenderManager();
    return render;
}

// 注册provider
void stds_gl_registerProvider(void *hander, const void *provider) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    std::shared_ptr<STDSProvider> CppProvider = make_shared<STDSProvider>((STDRenderEngineProvider *)provider);
    render->setRegisterProvider(CppProvider);
}

// 销毁引擎
void stds_gl_destroyRenderEngine(void *hander) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    delete render;
}

/// -------------------------------------------------------------- 合成相关 --------------------------------------------------------------
// 设置合成宽高
void stds_gl_setComposeSizeSRC(void *hander, int width, int height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setSizeSRC(width, height);
}

// 设置合成持续时间，duration 时长
void stds_gl_setComposeDuration(void *hander, float duration) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setComposeDuration(duration);
}

void stds_gl_testGl(void *hander) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->test();
}

/// -------------------------------------------------------------- 图层相关 --------------------------------------------------------------
// 添加图层
void stds_gl_addLayer(void *hander, int layerID, int type) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->addLayer(layerID, type);
}

void stds_gl_moveLayerToCompose(void *hander, int layerID, int composeID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->moveLayerToCompose(layerID, composeID);
}

// 移除图层
void stds_gl_removeLayer(void *hander, int layerID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->removeLayer(layerID);
}

void stds_gl_setSolidLayerColor(void *hander, int layerId, float r, float g, float b) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setSolidLayerColor(layerId, r, g, b);
}

// 将 layerID 图层移动到 targetLayerID 图层之上
void stds_gl_moveLayerToFront(void *hander, int composeID, int layerID, int targetLayerID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->moveLayerToFront(composeID, layerID, targetLayerID);
}

// 将layerID 图层 移动到 targetLayerID 图层之下
void stds_gl_moveLayerToBack(void *hander, int composeID, int layerID, int targetLayerID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->moveLayerToBack(composeID, layerID, targetLayerID);
}

// 将图层移动到最上面
void stds_gl_moveLayerToTop(void *hander, int composeID, int layerID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->moveLayerToTop(composeID, layerID);
}

// 将图层移动到最下面
void stds_gl_moveLayerToBottom(void *hander, int composeID, int layerID) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->moveLayerToBottom(composeID, layerID);
}

// 设置图层显示状态,0 不显示，1 显示
void stds_gl_visibleLayer(void *hander, int layerID, int isVisible) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->visibleLayer(layerID, isVisible);
}

// 设置图层生命周期，start 开始时间，duration 时长
void stds_gl_setLayerLife(void *hander, int layerID, float start, float duration) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerLife(layerID, start, duration);
}

// 设置图层的透明度，alpha 0～1
void stds_gl_setLayerAlpha(void *hander, int layerID, float alpha) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerAlpha(layerID, alpha);
}

// 设置图层的混合模式
void stds_gl_setLayerBlend(void *hander, int layerID, int blendType) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerBlend(layerID, blendType);
}

// 设置图层大小
void stds_gl_setLayerSize(void *hander, int layerID, float w, float h) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerSize(layerID, w, h);
}

// 设置图层中心位置
void stds_gl_setLayerCenter(void *hander, int layerID, float x, float y) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerCenter(layerID, x, y);
}

// 设置图层缩放
void stds_gl_setLayerScale(void *hander, int layerID, float x, float y) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerScale(layerID, x, y);
}

// 设置图层旋转角度
void stds_gl_setLayerRotate(void *hander, int layerID, float angle) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerRotate(layerID, angle);
}

// 设置图层是否自动大小
void stds_gl_setLayerAutoSize(void *hander, int layerID, bool autoW, bool autoH) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setLayerAutoSize(layerID, autoW, autoH);
}

// 仿射变换-平移
void stds_gl_transformation_translationLayer(void *hander, int layerID, float dx, float dy) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->transformationTranslation(layerID, dx, dy);
}

// 放射变换-缩放
void stds_gl_transformation_scaleLayer(void *hander, int layerID, float dx, float dy) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->transformationScale(layerID, dx, dy);
}

// 仿射变换-旋转
void stds_gl_transformation_rotationLayer(void *hander, int layerID, float dAngle) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->transformationRotation(layerID, dAngle);
}

/// -------------------------------------------------------------- 效果相关 --------------------------------------------------------------
// 给图层添加效果， effectType 效果类型
void stds_gl_addEffect(void *hander, int layerID, int effectType) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->addEffect(layerID, effectType);
}

// 移除效果，effectType 效果类型
void stds_gl_removeEffect(void *hander, int layerID, int effectType) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->removeEffect(layerID, effectType);
}

// 调整效果的参数强度，effectType 效果类型，paramName 效果的参数名称, intensity 强度
void stds_gl_changeEffectFloatValue(void *hander, int layerID, int effectType, const char *paramName, float *floatValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || paramName == nullptr || floatValue == nullptr) {
        return;
    }
    render->changeEffectFloatValue(layerID, effectType, paramName, floatValue, n);
}

void stds_gl_changeEffectIntValue(void *hander, int layerID, int effectType, const char *paramName, int *intValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || paramName == nullptr || intValue == nullptr) {
        return;
    }
    render->changeEffectIntValue(layerID, effectType, paramName, intValue, n);
}

void stds_gl_changeEffectStringValue(void *hander, int layerID, int effectType, const char *paramName, const char *stringValue) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || paramName == nullptr || stringValue == nullptr) {
        return;
    }
    render->changeEffectStringValue(layerID, effectType, paramName, stringValue);
}

void stds_gl_changeEffectBitmap(void *hander, int layerID, int effectType, const char *paramName, const char *data, int width, int height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || paramName == nullptr || data == nullptr) {
        return;
    }
    render->changeEffectBitmap(layerID, effectType, paramName, data, width, height);
}

void stds_gl_changeEffectTex(void *hander, int layerID, int effectType, const char *paramName, unsigned int texId, int width, int height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || paramName == nullptr) {
        return;
    }
    render->changeEffectTex(layerID, effectType, paramName, texId, width, height);
}

// 设置效果显示状态,isVisible 0 不显示，1 显示，effectType 效果类型
void stds_gl_visibleEffect(void *hander, int layerID, int effectType, int isVisible) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->visibleEffect(layerID, effectType, isVisible);
}

bool stds_gl_checkEffectStatus(void *hander, int layerID, int effectType, const char *label) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return false;
    }
    return render->checkEffectStatus(layerID, effectType, label);
}

/// -------------------------------------------------------------- 热加载效果 --------------------------------------------------------------
void stds_gl_addHotEffect(void *hander, int layerID, const char *effectName, const char *effectRoot) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr || effectRoot == nullptr) {
        return;
    }
    render->addHotEffect(layerID, effectName, effectRoot);
}

// 移除效果，effectName 热加载效果名称
void stds_gl_removeHotEffect(void *hander, int layerID, const char *effectName) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr) {
        return;
    }
    render->removeHotEffect(layerID, effectName);
}

// 给热加载效果设置参数
void stds_gl_changeHotEffectFloatValue(void *hander, int layerID, const char *effectName, const char *paramName, float *floatValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr || paramName == nullptr || floatValue == nullptr) {
        return;
    }
    render->changeHotEffectFloatValue(layerID, effectName, paramName, floatValue, n);
}

void stds_gl_changeHotEffectIntValue(void *hander, int layerID, const char *effectName, const char *paramName, int *intValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr || paramName == nullptr || intValue == nullptr) {
        return;
    }
    render->changeHotEffectIntValue(layerID, effectName, paramName, intValue, n);
}

void stds_gl_changeHotEffectStringValue(void *hander, int layerID, const char *effectName, const char *paramName, const char *stringValue) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr || paramName == nullptr || stringValue == nullptr) {
        return;
    }
    render->changeHotEffectStringValue(layerID, effectName, paramName, stringValue);
}

// 是否显示热加载效果
void stds_gl_visibleHotEffect(void *hander, int layerID, const char *effectName, int isVisible) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || effectName == nullptr) {
        return;
    }
    render->visibleHotEffect(layerID, effectName, isVisible);
}

void stds_gl_addHotFilter(void *hander, int layerID, const char *filter, const char *effectRoot) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr || effectRoot == nullptr) {
        return;
    }
    render->addHotFilter(layerID, filter, effectRoot);
}

// 移除效果，effectName 热加载效果名称
void stds_gl_removeHotFilter(void *hander, int layerID, const char *filter) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr) {
        return;
    }
    render->removeHotFilter(layerID, filter);
}

// 给热加载效果设置参数
void stds_gl_changeHotFilterFloatValue(void *hander, int layerID, const char *filter, const char *effectName, const char *paramName, float *floatValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr || effectName  == nullptr || paramName == nullptr || floatValue == nullptr) {
        return;
    }
    render->changeHotFilterFloatValue(layerID, filter, effectName, paramName, floatValue, n);
}

void stds_gl_changeHotFilterIntValue(void *hander, int layerID, const char *filter, const char *effectName, const char *paramName, int *intValue, int n) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr || effectName  == nullptr || paramName == nullptr || intValue == nullptr) {
        return;
    }
    render->changeHotFilterIntValue(layerID, filter, effectName, paramName, intValue, n);
}

void stds_gl_changeHotFilterStringValue(void *hander, int layerID, const char *filter, const char *effectName, const char *paramName, const char *stringValue) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr || effectName  == nullptr || paramName == nullptr || stringValue == nullptr) {
        return;
    }
    render->changeHotFilterStringValue(layerID, filter, effectName, paramName, stringValue);
}

// 是否显示热加载效果
void stds_gl_visibleHotFilter(void *hander, int layerID, const char *filter, int isVisible) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || filter == nullptr) {
        return;
    }
    render->visibleHotFilter(layerID, filter, isVisible);
}

/// -------------------------------------------------------------- 渲染相关 --------------------------------------------------------------
// 设置进度
void stds_gl_setCurrentPrograss(void *hander, float prograss) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->setCurrentPrograss(prograss);
}

// 渲染
void stds_gl_render(void *hander) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->render();
}

void stds_gl_readPixel(void *hander, int *width, int *height, unsigned char **data) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->readPixel(width, height, data);
}

/// -------------------------------------------------------------- 替换资源 --------------------------------------------------------------
// 替换图片资源
void stds_gl_replaceImageAsset(void *hander, int layerId, int assetId, const char *path) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || path == nullptr) {
        return;
    }
    render->replaceImageAsset(layerId, assetId, path);
}

// 替换纹理
void stds_gl_replaceImageTex(void *hander, int layerId, int assetId, const unsigned int textureId, float width, float height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->replaceImageTex(layerId, assetId, textureId, width, height);
}

// 替换图片数据
void stds_gl_replaceImageData(void *hander, int layerId, int assetId, const unsigned char *data, float width, float height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || data == nullptr) {
        return;
    }
    render->replaceImageData(layerId, assetId, data, width, height);
}

void stds_gl_replaceMaskAsset(void *hander, int layerId, int assetId, const char *path) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || path == nullptr) {
        return;
    }
    render->replaceMaskAsset(layerId, assetId, path);
}

void stds_gl_replaceMaskTex(void *hander, int layerId, int assetId, const unsigned int textureId, float width, float height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr) {
        return;
    }
    render->replaceMaskTex(layerId, assetId, textureId, width, height);
}

void stds_gl_replaceMaskData(void *hander, int layerId, int assetId, const unsigned char *data, float width, float height) {
    STDRenderManager *render = (STDRenderManager *)hander;
    if (render == nullptr || data == nullptr) {
        return;
    }
    render->replaceMaskData(layerId, assetId, data, width, height);
}
